<?php
/**
 * Created by PhpStorm.
 * User: man0sions
 * Date: 16/8/25
 * Time: 下午6:20
 */

namespace src\base;

/**
 * 命令解释器
 * 负责把命令行参数解析到相应的command
 * Class CommandResolver
 * @package src\base
 * @author Luficer.p <81434146@qq.com>
 */
class CommandResolver
{
    private $cmd;

    /**
     * 命令行下Request对象
     * array(4) {
     *       [0]=>
     *       string(9) "index.php"
     *       [1]=>
     *       string(3) "aaa"
     *       [2]=>
     *       string(3) "bbb"
     *       [3]=>
     *       string(3) "ccc"
     *       }
     *
     * CommandResolver constructor.
     * param Request $request
     */
    function __construct(Request $request)
    {
        $cmd = $request::get(1);
        if(!$cmd)
            $this->showUseage();
        $class = "\\src\\commands\\".ucfirst($cmd);
        if(!class_exists($class))
            throw new AppException("{$class} not exists");

        $this->cmd = $class;

    }

    /**
     * @return string
     */
    function getCommand(){
        return  new $this->cmd;
    }

    /**
     *
     */
    function showUseage(){
        echo "useage: php index.php [arguments]\n";
        echo "arguments\n";

    }

}